package io.wongxd.solution.compose.custom.views.value_selector.date_time_selector

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import io.wongxd.solution.compose.custom.views.value_selector.WValueSelectState
import java.util.Calendar

/**
 * effect : [WDateTimeSelector] 的状态
 *          State of the [WDateTimeSelector]
 *
 * @param defaultXxx 默认选中的日期
 *                   Default selected date
 * @param minXxx 最小可选择日期
 *                Minimum selected date
 * @param maxXxx 最大可选择日期
 *                Maximum selected date
 */
class WDateTimeSelectorState(
    internal val defaultYear: Int,
    internal val defaultMonth: Int,
    internal val defaultDay: Int,
    internal val defaultHour: Int = 0,
    internal val defaultMinute: Int = 0,
    internal val defaultSecond: Int = 0,
    minYear: Int = 1900,
    maxYear: Int = 2100,
) {
    val yearState = WValueSelectState()
    val monthState = WValueSelectState()
    val dayState = WValueSelectState()

    internal var years by mutableStateOf(arrayOf(*((minYear..maxYear).map(Int::toString)).toTypedArray()))
    internal var months by mutableStateOf(arrayOf(*(1..12).map(Int::toString).toTypedArray()))
    internal var days by mutableStateOf(arrayOf(*(1..31).map(Int::toString).toTypedArray()))

    fun getYear(): String = years[yearState.getSelectIndex()]
    fun getMonth(): String = months[monthState.getSelectIndex()]
    fun getDay(): String = days[dayState.getSelectIndex()]

    val hourState = WValueSelectState()
    val minuteState = WValueSelectState()
    val secondState = WValueSelectState()

    internal var hours by mutableStateOf(arrayOf(*(0 until 24).map(Int::toString).toTypedArray()))
    internal var minutes by mutableStateOf(arrayOf(*(0 until 60).map(Int::toString).toTypedArray()))
    internal var seconds by mutableStateOf(arrayOf(*(0 until 60).map(Int::toString).toTypedArray()))

    fun getHour(): String = hours[hourState.getSelectIndex()]
    fun getMinute(): String = minutes[minuteState.getSelectIndex()]
    fun getSecond(): String = seconds[secondState.getSelectIndex()]

    fun getMill(): Long = Calendar.getInstance().apply {
        set(
            getYear().toInt(),
            getMonth().toInt(),
            getDay().toInt(),
            getHour().toInt(),
            getMinute().toInt(),
            getSecond().toInt()
        )
    }.timeInMillis

}